<?php
/**
 * SHOP 
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act']=='list'){
	$qy_id=isset($_REQUEST['qy'])?$_REQUEST['qy']:0;
	$pei_id=isset($_REQUEST['pei'])?$_REQUEST['pei']:0;
	$keywords=isset($_REQUEST['keywords'])?$_REQUEST['keywords']:0;
	$num='10';
	//所有区域↓
	$sql="select DISTINCT r_name,r_id from ".ly()."shop_region where r_del='0'";
	$row=$db->getALL($sql);
	//所有区域↑
	$sql="select a.user_id,a.user_name,a.user_shid,a.mobile_phone,a.email,b.shop_name from ".ly()."users a, ".ly()."shop b ";
	$where=" where a.user_shid=b.s_id  ";
	//列表↓
	$where.="and a.user_shid>'0' ";
	//列表↑
	if($qy_id){
		$where.="and b.r_id='{$qy_id}'";
	}
	if($pei_id){
		$where.="and a.user_shid='{$pei_id}'";
	}
	if($keywords){
		$where.="and a.user_name like '%{$keywords}%'";
	}
	$sql.=$where;
	//分页
	$num='10';
	$e=$db->query($sql);
	$total_count=$db->num_rows($e);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;

	$res=$db->getALL($sql);
	$smarty->assign('row',$row);
	$smarty->assign('res',$res);
	$aa ="select * FROM ".ly()."shop where  del_status=0 and r_id='{$qy_id}'";
	$aa=$db->getALL($aa);
	$smarty->assign('p_list',$aa);
	$smarty->display('peisong_list.html');
}elseif($_REQUEST['act']=='edit'){
	$user_id=$_REQUEST['user_id'];
	$sql="select s_id,shop_name from ".ly()."shop where del_status='0'and shop_zt='0'";
	$res=$db->getAll($sql);
	if($user_id>'0'){
		$sql1="select a.user_shid,a.user_id,a.user_name,a.mobile_phone,a.email,b.shop_name from ".ly()."users a, ".ly()."shop b where a.user_shid=b.s_id  and a.user_id='{$user_id}'";
		$res1=$db->getRow($sql1);
	}
	$smarty->assign('res',$res);
	$smarty->assign('res1',$res1);
	$smarty->display('peisong_edit.html');
}elseif($_REQUEST['act']=='insert'){
	$user_id=$_REQUEST['user_id'];
	$s_id=$_REQUEST['s_id'];
	$user_name=$_REQUEST['user_name'];
	$sql="select user_id from ".ly()."users  where user_shid='{$s_id}' and user_id='{$user_id}'";
	$res=$db->getAll($sql);
	$user=$res['0']['user_id'];
	if($user>0){
		echo"<script>alert('该配送员已在此店铺');history.go(-1);</script>";
	}
	if($user_id>'0'){
		$sql="update ".ly()."users set user_shid='{$s_id}' where user_id='{$user_id}'";
		$res=$db->query($sql);
	}else{
		$sql="update ".ly()."users set user_shid='{$s_id}' where user_name='{$user_name}'";
		$res=$db->query($sql);
	}
	if($res=='1'){
		$href[]=array('text'=>"返回配送员列表",'href'=>'peisong.php?act=list');
         sys_msg("操作成功！",$href);
     }else{
     	$href[]=array('text'=>"重新编辑",'href'=>'peisong.php?act=edit');
         sys_msg("操作失败！",$href);
     }
}elseif($_REQUEST['act']=='del'){
	$user_id=$_REQUEST['user_id'];
	$sql="update ".ly()."users set user_shid='0' where user_id='{$user_id}'";
	$res=$db->query($sql);
	if($res=='1'){
		$href[]=array('text'=>"返回配送员列表",'href'=>'peisong.php?act=list');
         sys_msg("操作成功！",$href);
	}else{
		$href[]=array('text'=>"返回配送员列表",'href'=>'peisong.php?act=list');
         sys_msg("操作失败！",$href);
	}
}elseif($_REQUEST['act']=='name'){
	$user_name=$_REQUEST['user_name'];
	if(empty($user_name)){
		echo '4';
		exit;
	}
	$sql="select user_id,user_shid from ".ly()."users  where user_name='{$user_name}'";
	$res=$db->getAll($sql);
	$row=$res['0']['user_shid'];
	if(empty($res)){
		echo '1';
	}else if($row>0){
		echo '3';
	}else{
		echo '2';
	}

}elseif($_REQUEST['act']=='peaj'){
	$r_id=$_POST['id'];
	$sql ="select s_id,shop_name FROM ".ly()."shop where  del_status=0 and r_id='{$r_id}'";
	$re = $GLOBALS['db']->query($sql);
	while($rows = $GLOBALS['db']->fetchRow($re)){
		echo "<option value='".$rows['s_id']."'>".$rows['shop_name']."</option>";
	}
}
?>